package com.dpiinc.TechPage.user.setting;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ListView;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.model.jbean.NoteLabelEditBean;
import com.dpiinc.TechPage.pen.adapter.LabelBaseAdapter;
import com.dpiinc.TechPage.pen.dialog.OneButtonDialog;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.room.bean.LabelEntity;
import com.dpiinc.TechPage.room.dao.LabelDao;
import com.dpiinc.TechPage.user.logic.NoteLabelLogic;
import com.dpiinc.TechPage.user.utils.ToastUtils;
import com.dpiinc.TechPage.widget.ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by hu489_000 on 2018-07-22.
 */

public class LabelAvtivity extends BaseActivity {

    private boolean isEdit = false;

    ClearEditText add_label_edit;
    List<LabelEntity> list = new ArrayList<LabelEntity>();
    Button add_label;
    LabelDao labelDao;
    LabelBaseAdapter mLabelBaseAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.label_main);
        super.onCreate(savedInstanceState);

        setBackWhite();
        setstatusBarColor(R.color.color_00bad2);

        labelDao = RoomAiWriterDatabase.getInstance(this).labelDao();

        add_label = (Button) findViewById(R.id.add_label);
        add_label.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeInput();
                if (!TextUtils.isEmpty(add_label_edit.getText())) {
                    if (list.size() > 9) {
                        ToastUtils.showCenterTip(LabelAvtivity.this, getString(R.string.toast_max_label_count_limit));
                        return;
                    }
                    LabelEntity le = labelDao.getLabelByContent(add_label_edit.getText().toString());
                    if (le != null) {
                        OneButtonDialog.ShowDialog(LabelAvtivity.this, null, getString(R.string.label_name_exist), true, null);
                        return;
                    }
                    NoteLabelLogic.noteLabelEdit(LabelAvtivity.this, NoteLabelLogic.EDITTYPE_ADD, null, add_label_edit.getText().toString(), new NoteLabelLogic.OnEditSuccess() {
                        @Override
                        public void onHandleSuccess(NoteLabelEditBean baseBean) {
                            LabelEntity label = new LabelEntity(baseBean.getLabelId(), add_label_edit.getText().toString());
                            labelDao.insert(label);
                            add_label_edit.setText("");
                            notifyDataSetChanged();
                            ToastUtils.showCenterTip(LabelAvtivity.this, getString(R.string.add_label_success));
                        }
                    });
                } else {
                    OneButtonDialog.ShowDialog(LabelAvtivity.this, null, getString(R.string.label_name_not_null), true, null);
                }
            }
        });

        add_label_edit = findViewById(R.id.add_label_edit);
        add_label_edit.setPhoneInput(false);


        NoteLabelLogic.getNoteLabel(LabelAvtivity.this, new NoteLabelLogic.OnGetSuccess() {
            @Override
            public void onHandleSuccess() {
                showUi();
            }
        });
    }

    private void showUi() {

//        labelDao = RoomAiWriterDatabase.getInstance(this).labelDao();
        getAllLabels();

        mLabelBaseAdapter = new LabelBaseAdapter(LabelAvtivity.this, list);
        ListView listView = (ListView) findViewById(R.id.list_view_label);
        listView.setAdapter(mLabelBaseAdapter);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        closeInput();
        finish();
    }

    private void getAllLabels() {
        list.clear();
        list.addAll(labelDao.getLabels());
    }

    public void notifyDataSetChanged() {
        getAllLabels();
        mLabelBaseAdapter.notifyDataSetChanged();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_label, menu);
        if (isEdit) {
            menu.getItem(0).setTitle(R.string.menu_edit_complite);
        } else {
            menu.getItem(0).setTitle(R.string.menu_edit);
        }
        MenuItem item = menu.findItem(R.id.menu_eidt);
        SpannableString spannableString = new SpannableString(item.getTitle());
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.clear_note_dialog_btn_text_color_left)), 0, spannableString.length(), 0);
        item.setTitle(spannableString);
        return true;
    }


    private void closeInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
//            InputMethodManager inputMethodManager =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            inputMethodManager.hideSoftInputFromWindow(LabelAvtivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            imm.hideSoftInputFromWindow(add_label_edit.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case R.id.menu_eidt:
                isEdit = !isEdit;
                mLabelBaseAdapter.setEdit(isEdit);
                invalidateOptionsMenu();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

}
